package managedbean.CRM.SaleOrder; 
 
import entity.CRM.LineItem;
import entity.CRM.SaleOrder;
import entity.Employee;
import javax.faces.event.ActionEvent;
import java.io.IOException;
import java.util.List; 
import javax.ejb.EJB; 
import javax.ejb.EJBException;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext;
import managedbean.Controller;
import session.stateless.CRM.SaleOrderBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;
 
@ManagedBean(name="viewSaleOrder")
@ViewScoped 
public class ViewSaleOrderManagerBean { 
    @EJB 
    private SaleOrderBeanLocal saleOrderBeanLocal; 
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    
    private SaleOrder selectedSaleOrder;
    private List<LineItem> lineItems;
    private String statusMessage;
    
    public ViewSaleOrderManagerBean() { 
    } 
 
 
    public List<SaleOrder> getSaleOrders() throws EJBException
    { 
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        Employee executive = controller.getEmployee();
        List<SaleOrder> temp = saleOrderBeanLocal.getSaleOrders(executive.getEmployeeId());
        
        return temp;

    } 
    
    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public SaleOrderBeanLocal getSaleOrderBeanLocal() {
        return saleOrderBeanLocal;
    }

    public void setSaleOrderBeanLocal(SaleOrderBeanLocal saleOrderBeanLocal) {
        this.saleOrderBeanLocal = saleOrderBeanLocal;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    
        
    public void editSaleOrder(ActionEvent event) throws IOException
    {
        selectedSaleOrder = this.getSelectedSaleOrder();
        if(selectedSaleOrder != null)
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleOrder", selectedSaleOrder);
            FacesContext.getCurrentInstance().getExternalContext().redirect("editSaleOrder.xhtml");      
        }
    }
    
    
    
    public void deleteSaleOrder(ActionEvent event) throws IOException
    {
       selectedSaleOrder = this.getSelectedSaleOrder();

        try
        {
            saleOrderBeanLocal.deleteSaleOrder(selectedSaleOrder.getId());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleOrder.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void checkCredit(ActionEvent event) throws IOException
    {
        selectedSaleOrder = this.getSelectedSaleOrder();
        if (selectedSaleOrder.getStateSaleOrder()==SaleOrder.StateSaleOrder.Confirmed)        
            statusMessage="Invalid: Order confirmed previously";
        else
        {
            Double temp =creditAccountBeanLocal.isPass_checkCredit(selectedSaleOrder.getId());
            if (temp>0)
                statusMessage = "Pass!";
            else
                statusMessage ="Fail: Lack $ "+temp+"";
        }
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + "." , ""));         
        
    }
    public void confirmOrder (ActionEvent event) throws IOException
    {
        selectedSaleOrder = this.getSelectedSaleOrder();
        if (selectedSaleOrder.getStateSaleOrder()==SaleOrder.StateSaleOrder.Confirmed)
            statusMessage="Invalid: Order confirmed previously";
        else
        {
            Double temp =creditAccountBeanLocal.isPass_checkCredit(selectedSaleOrder.getId());
            if (temp>0)
            {
                saleOrderBeanLocal.updateStatus(selectedSaleOrder.getId(), SaleOrder.StateSaleOrder.Confirmed);
                creditAccountBeanLocal.modifyCreditAccount_AddConfirmedSaleOrder(selectedSaleOrder.getId());
                statusMessage="Order has been confirmed. Outstanding credit has been updated to "+saleOrderBeanLocal.findSaleOrder(selectedSaleOrder.getId()).getCustomer().getCreditAccount().getOutstandingCredit();
            }
            else
            {
                statusMessage="Invalid: Credit check failed.";
            }
        }
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + "" , ""));         
    }
} 